package dp;

import java.util.Arrays;

/**
 * 91. 解码方法
 */
public class P91 {

    public int numDecodings(String s) {
        if (s.startsWith("0")) {
            return 0;
        } else if (s.length() == 1) {
            return 1;
        }
        int[] dp = new int[s.length() + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            if ('0' == c) {
                char preC = s.charAt(i - 1);
                if (preC == '0') {
                    return 0;
                }
                dp[i + 1] = dp[i - 1];
            } else {
                char preC = s.charAt(i - 1);
                String sub = "" + preC + c;
                if (sub.compareTo("10")>0 && sub.compareTo("26")<=0) {
                    dp[i + 1] = dp[i] + dp[i - 1];
                } else {
                    dp[i + 1] = dp[i];
                }
            }
        }
        System.out.print(" ,");
        Arrays.stream(s.split("")).forEach(a -> System.out.print(a + ","));
        System.out.println();
        Arrays.stream(dp).forEach(a -> System.out.print(a + ","));
        System.out.println();
        return dp[s.length()];
    }


    public static void main(String[] args) {
        int count = new P91().numDecodings("2611055971756562");
        System.out.println(count);
    }
}
